package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.task.TaskQuery;
import org.quincy.rock.core.bean.BeanUtil;
import org.quincy.rock.core.util.CoreUtil;
import org.quincy.rock.core.util.DateUtil;
import org.quincy.rock.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * <b>CamundaRest。</b>
 * <p><b>详细说明：</b></p>
 * <!-- 在此添加详细说明 -->
 * 无。
 * 
 * @version 1.0
 * @author wks
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/bpmn")
public class CamundaRest {

	@Autowired
	private RepositoryService repoService;

	@Autowired
	private RuntimeService runService;

	@Autowired
	private IdentityService idService;
	
	@Autowired
	private TaskService taskService;
	
	@RequestMapping(value = "/deploy", method = RequestMethod.GET)
	public boolean deploy() {
		Deployment deploy = repoService.createDeployment().addClasspathResource("processes/leave.bpmn").name("leave")
				.deploy();
		log.info(deploy.getName());
		return true;
	}

	@RequestMapping(value = "/findAllDeploy", method = RequestMethod.GET)
	public List<String> findAllDeploy() {
		List<String> list = new ArrayList<String>();
		repoService.createDeploymentQuery().list().forEach((ele) -> list.add(ele.getId() + "," + ele.getName()));
		return list;
	}

	@RequestMapping(value = "/findAllProcessDefinition", method = RequestMethod.GET)
	public List<String> findAllProcessDefinition() {
		List<ProcessDefinition> list = repoService.createProcessDefinitionQuery().list();
		List<String> relist = new ArrayList<String>();
		list.forEach((pd) -> relist.add(pd.getId() + "," + pd.getKey() + "," + pd.getName()));
		return relist;
	}

	/**
	 * <b>开始一个流程。</b>
	 * <p><b>详细说明：</b></p>
	 * <!-- 在此添加详细说明 -->
	 * 无。
	 * @param key 流程定义id
	 * @return
	 */
	@RequestMapping(value = "/start", method = RequestMethod.GET)
	public boolean start(String key) {
		//idService.setAuthenticatedUserId("admin");
		ProcessInstance pi = runService.startProcessInstanceById(key);
		log.info("pid:{},ProcessDefinitionId:{},businessKey:{}", pi.getProcessInstanceId(), pi.getProcessDefinitionId(),
				pi.getBusinessKey());
		return true;
	}
	
	@RequestMapping(value = "/findAllTask", method = RequestMethod.GET)
	public List<String> findAllTask(String id){
		List<String> list=new ArrayList<String>();
		taskService.createTaskQuery().list().forEach((ele)->list.add(ele.getId()+","+ele.getName()+","+ele.getAssignee()));
		return list;
	}
	
	/**
	 * <b>获得任务进度。</b>
	 * <p><b>详细说明：</b></p>
	 * <!-- 在此添加详细说明 -->
	 * 无。
	 * @param instanceId
	 * @return
	 */
	@RequestMapping(value = "/findAllActivityTask", method = RequestMethod.GET)
	public List<Map<String, Object>> getActivityTask(String instanceId){
		List<Map<String, Object>> relist=new ArrayList<Map<String,Object>>();
		TaskQuery query=taskService.createTaskQuery().active();
		if (StringUtils.isNotBlank(instanceId)) {
			query.processInstanceId(instanceId);
		}
		List<Task> list=query.list();
		for (Task task:list) {			
			//分配任务处理人
			taskService.addCandidateUser( task.getId(), "wks");
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("taskId", task.getId());
			map.put("execId", task.getExecutionId());
			map.put("procDefId", task.getProcessDefinitionId());	
			map.put("instanceId", task.getProcessInstanceId());
			map.put("taskDefKey", task.getTaskDefinitionKey());	
			map.put("name", task.getName());
			map.put("assignee", task.getAssignee());			
			map.put("createTime", DateUtil.formatDateTime(task.getCreateTime()));
			map.put("followUpDate", DateUtil.formatDateTime(task.getFollowUpDate()));
			map.put("dueDate", DateUtil.formatDateTime(task.getDueDate()));
			
			relist.add(map);
		}		
		return relist;
	}
	
	/**
	 * <b>完成任务。</b>
	 * <p><b>详细说明：</b></p>
	 * <!-- 在此添加详细说明 -->
	 * 无。
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/completeTask", method = RequestMethod.GET)
	public boolean completeTask(String taskId) {
		taskService.complete(taskId);
		return true;
	}
}
